﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;

namespace WoodManager.UI
{
    public partial class ModuleVan : Form
    {
        ConnectManager cm = new ConnectManager();
        Validator vali = new Validator();

        DataTable tbNhomModule, tbModule;

        string sModuleIdOld = "";
        int iRowIndex = 0;

        public ModuleVan()
        {
            InitializeComponent();           
        }

        private void loadTreeVIew()
        {
            DataTable tbNhom;
            string ret = "";

            if (cm.conn.State == ConnectionState.Closed)
                cm.conn.Open();
            treeView1.Nodes.Clear();
            tbNhom = cm.getTable("Select * from NhomModule");

            TreeNode tnLevel1;
            for (int i = 0; i < tbNhom.Rows.Count; i++)
            {
                DataTable tbPhanNhom = cm.getTable("Select * from PhanNhomModule where NhomID = N'" 
                                            + tbNhom.Rows[i]["NhomModuleID"].ToString() + "'");

                if (tbPhanNhom.Rows.Count > 0)
                {
                    TreeNode[] tnLevel2 = new TreeNode[tbPhanNhom.Rows.Count];
                    for (int i2 = 0; i2 < tbPhanNhom.Rows.Count; i2++)
                    {                        
                        tnLevel2[i2] = new TreeNode(tbPhanNhom.Rows[i2]["TenPhanNhom"].ToString());
                        tnLevel2[i2].Name = tbPhanNhom.Rows[i2]["PhanNhomID"].ToString();
                    }
                    tnLevel1 = new TreeNode(tbNhom.Rows[i]["TenNhomModule"].ToString(), tnLevel2);
                }                
                else
                {
                    tnLevel1 = new TreeNode(tbNhom.Rows[i]["TenNhomModule"].ToString());
                }
                tnLevel1.Name = tbNhom.Rows[i]["NhomModuleID"].ToString();
                treeView1.Nodes.Add(tnLevel1);
            }
            if (cm.conn.State == ConnectionState.Open)
                cm.conn.Close();
        }

        private void ModuleVan_Load(object sender, EventArgs e)
        {
            loadTreeVIew();
        }

        private void loadModuleData( string sNhomModule, int iLevel )
        {
            if (iLevel == 0)
            {
                tbModule = cm.getTable("select * from ViewModule where NhomModuleID = N'"+ sNhomModule +"'");
                dtgModule.DataSource = tbModule;
            }
            else
            {
                tbModule = cm.getTable("select * from ViewModule where PhanNhomID = N'" + sNhomModule + "'");
                dtgModule.DataSource = tbModule;
            }
        }


        private void dtgModule_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;

            txtTenModule.Text = this.tbModule.Rows[e.RowIndex]["TenModule"].ToString();
            this.txtMaModule.Text = tbModule.Rows[e.RowIndex]["ModuleID"].ToString();
            txtGhiChu.Text = tbModule.Rows[e.RowIndex]["GhiChu"].ToString();
            sModuleIdOld = txtMaModule.Text;
            iRowIndex = e.RowIndex;
            btnDelete.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if ((txtMaModule.Text == "") || (txtTenModule.Text == ""))
                {
                    MessageBox.Show("Thông tin không được để trống");
                    return;
                }

                if (vali.checkDuplicate(txtMaModule.Text, tbModule, 0))
                {
                    MessageBox.Show("Đã có module sử dụng mã này trong database");
                    return;
                }
                string sInsert = "InsertModule N'"+txtMaModule.Text+"',N'"+txtTenModule.Text+"',N'"
                    +treeView1.SelectedNode.Name +"',N'"+txtGhiChu.Text+"'";

                SqlCommand sqlInsert = new SqlCommand(sInsert, cm.conn);

                if (cm.conn.State == ConnectionState.Closed)
                    cm.conn.Open();

                sqlInsert.ExecuteNonQuery();
                loadModuleData( treeView1.SelectedNode.Name , 1);

                if (cm.conn.State == ConnectionState.Open)
                    cm.conn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {

                if ((txtMaModule.Text == "") || (txtTenModule.Text == ""))
                {
                    MessageBox.Show("Thông tin không được để trống");
                    return;
                }

                if (vali.checkDuplicate(txtMaModule.Text, tbModule, 0) 
                    && (txtMaModule.Text != sModuleIdOld))
                {
                    MessageBox.Show("Không có mã này trong database");
                    return;
                }
                string sUpdate = "UpdateModule N'" + txtMaModule.Text + "',N'" + txtTenModule.Text 
                            + "',N'" + treeView1.SelectedNode.Name
                            + "',N'" + txtGhiChu.Text + "', N'"+sModuleIdOld+"'";

                SqlCommand sqlUpdate = new SqlCommand(sUpdate, cm.conn);

                if (cm.conn.State == ConnectionState.Closed)
                    cm.conn.Open();

                sqlUpdate.ExecuteNonQuery();

                loadModuleData( treeView1.SelectedNode.Name , 1);

                if (cm.conn.State == ConnectionState.Open)
                    cm.conn.Close();
                

                dtgModule.Rows[iRowIndex].Selected = true;
                sModuleIdOld = txtMaModule.Text;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (sModuleIdOld == "")
                {
                    MessageBox.Show("Chưa chọn module cần xóa");
                    return;
                }
                if (MessageBox.Show("Bạn có muốn xóa module '"+ sModuleIdOld +"' ko?","Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    SqlCommand sqlDel = new SqlCommand("Delete Module where ModuleID = N'" + sModuleIdOld + "'", cm.conn);
                    if (cm.conn.State == ConnectionState.Closed)
                        cm.conn.Open();
                    sqlDel.ExecuteNonQuery();
                    btnDelete.Enabled = false;
                    txtMaModule.Text = "";
                    txtTenModule.Text = "";
                    txtGhiChu.Text = "";
                    loadModuleData( treeView1.SelectedNode.Name , 1);
                    if (cm.conn.State == ConnectionState.Open)
                        cm.conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                btnAdd.Enabled = true;
                btnUpdate.Enabled = true;
                loadModuleData(e.Node.Name, 1);
            }
            else
            {
                btnAdd.Enabled = false;
                btnUpdate.Enabled = false;
                loadModuleData(e.Node.Name, 0);
            }
        }
    }
}
